// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_GAME_STATUS, ENUM_UI_TYPE } from "../Enum";
import AudioManager from "../manager/AudioManager";
import DataManager from "../manager/DataManager";
import GameManager from "../manager/GameManager";
import SdkManager from "../manager/SdkManager";
import { privacyChannel } from "../Script/privacyChannel";
import { privacyChannelOppo } from "../Script/privacyChannelOppo";
import { privacyChannelXM } from "../Script/privacyChannelXM";
import { StaticInstance } from "../StaticInstance";
import { random } from "../Utils";
import AddTimeLayer from "./AddTimeLayer";
import BaseLayer from "./Baselayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MainLayer extends BaseLayer {

    btnPause: cc.Node = null
    currentLevel: cc.Node = null
    time: cc.Node = null
    skills: cc.Node = null

    onLoad() {
        this.btnPause = cc.find('btn_pause', this.node)
        this.btnPause.on('click', this.onPauseClick, this)
        this.currentLevel = this.node.getChildByName('current_level')
        this.skills = this.node.getChildByName('skills')
        this.time = this.node.getChildByName("time")
        for (let i = 0; i < this.skills.children.length; i++) {
            const skill = this.skills.children[i]
            if (!skill.hasEventListener('click')) skill.on('click', () => { this.onSkillClick(i) }, this)
        }
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p3")) {
                    try {
                        this.skills.children[0].getChildByName("icon").active = false
                    } catch {
                        console.error("p3控制")
                    }
                } else {
                    this.skills.children[0].getChildByName("icon").active = false
                }

                //StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
                break

        }
        this.freshTool()
    }
    freshTool() {
        if (DataManager.instance.tool >= 1) {
            this.skills.children[0].getChildByName("icon").active = false
            this.skills.children[0].getChildByName("number").getComponent(cc.Label).string = DataManager.instance.tool + ""
        } else {
            this.skills.children[0].getChildByName("number").getComponent(cc.Label).string = ""
            switch (privacyChannel.channel) {
                case "oppo":
                case "oppo1":
                case "vivo":
                case "vivo1":
                case "xm":
                    if (privacyChannel.getChannel().getCtrl("p3")) {
                        try {
                            this.skills.children[0].getChildByName("icon").active = false
                        } catch {
                            console.error("p3控制")
                        }
                    } else {
                        this.skills.children[0].getChildByName("icon").active = true
                    }
                    //StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)

                    break
            }
        }
    }
    show(): void {
        super.show();
        // console.log("(privacyChannel.getChannel() as privacyChannelXM).showManyVideo()");
        // (privacyChannel.getChannel() as privacyChannelXM).showManyVideo()
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    privacyChannel.getChannel().showInter(true);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                if (DataManager.instance.stageModel) {
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AddBottleLayer)
                }
                break
            case "vivo":
            case "vivo1":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    privacyChannel.getChannel().showInter(true);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {
                    }
                }
                if (DataManager.instance.stageModel) {
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AddBottleLayer)
                    this.skills.children[2].active = false
                } else {
                    this.skills.children[2].active = true
                }
                break
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    privacyChannel.getChannel().showInter(true);
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                if (DataManager.instance.stageModel) {
                    StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AddBottleLayer)
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }


                this.skills.children[2].active = true
                this.skills.getComponent(cc.Widget).enabled = false
                this.skills.getComponent(cc.Layout).spacingX = 40
                this.skills.children[1].getChildByName("icon").active = true
                if (DataManager.instance.stageModel) {
                    this.skills.children[2].active = false
                    this.skills.setPosition(0, 432)

                } else {
                    this.skills.setPosition(0, -500)
                    this.skills.children[2].active = true

                }


                break
        }
    }
    onDestroy() {
        this.btnPause.off('click', this.onPauseClick, this)
    }

    onEnable() {
        this.rendorPowerTimer()
        SdkManager.instance.toggleBannerAd(true)
        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch (e) {
                        console.error("p1控制", e)
                    }
                }
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showBanner()
                        } catch (e) {
                            console.error("p1控制", e)

                        }

                    }, 5, cc.macro.REPEAT_FOREVER, 0)
                }
                if (privacyChannel.getChannel().getCtrl("p2")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showInter(true)
                        } catch {
                            console.error("p2控制")
                        }

                    }, this.getRandomNumber(15, 60), cc.macro.REPEAT_FOREVER, 0)
                }
                break
            case "vivo":
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch (e) {
                        console.error("p1控制", e)
                    }
                }
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showBanner()
                        } catch (e) {
                            console.error("p1控制", e)
                        }

                    }, 5, cc.macro.REPEAT_FOREVER, 0)
                }

                if (privacyChannel.getChannel().getCtrl("p2")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showInter(true)
                        } catch {
                            console.error("p2控制")
                        }

                    }, this.getRandomNumber(15, 60), cc.macro.REPEAT_FOREVER, 0)
                }

                if (privacyChannel.getChannel().getCtrl("p15")) {
                    this.schedule(() => {
                        if (DataManager.instance.level >= 2 || DataManager.instance.stageModel) {
                            try {
                                privacyChannel.getChannel().showVideo(() => {

                                })
                            } catch {
                                console.error("p15控制")
                            }
                        }

                    }, 8, cc.macro.REPEAT_FOREVER, 0)
                }
                break
            case "vivo1":
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch (e) {
                        console.error("p1控制", e)
                    }
                }
                if (privacyChannel.getChannel().getCtrl("p1")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showBanner()
                        } catch (e) {
                            console.error("p1控制", e)
                        }

                    }, 5, cc.macro.REPEAT_FOREVER, 0)
                }

                if (privacyChannel.getChannel().getCtrl("p2")) {
                    this.schedule(() => {
                        try {
                            privacyChannel.getChannel().showInter(true)
                        } catch {
                            console.error("p2控制")
                        }

                    }, this.getRandomNumber(15, 60), cc.macro.REPEAT_FOREVER, 0)
                }

                if (privacyChannel.getChannel().getCtrl("p15")) {
                    this.schedule(() => {
                        if (DataManager.instance.level >= 3 || DataManager.instance.stageModel) {
                            try {
                                privacyChannel.getChannel().showVideo(() => {

                                })
                            } catch {
                                console.error("p15控制")
                            }
                        }

                    }, 5, cc.macro.REPEAT_FOREVER, 0)
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p9") < Math.random() * 100) {
                    this.scheduleOnce(() => {
                        privacyChannel.getChannel().showInter()

                    }, random(30, 45))
                }
                break
        }
    }

    getRandomNumber(min: number, max: number): number {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }
    onDisable() {
        SdkManager.instance.toggleBannerAd(false)
    }

    onPauseClick() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.EXIT)
    }

    onSkillClick(i: number) {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        if (DataManager.instance.status == ENUM_GAME_STATUS.UNRUNING) return
        if (i == 0) {
            StaticInstance.gameManager.onGameUndo()
        } else if (i == 1) {
            if (this.skills.children[1].getChildByName("icon").active) {
                privacyChannel.getChannel().showVideo(() => {
                    StaticInstance.gameManager.onGameReset()
                })
            } else {
                StaticInstance.gameManager.onGameReset()
            }
        } else if (i == 2) {
            privacyChannel.getChannel().showVideo(() => {
                DataManager.instance.level += 1
                DataManager.instance.save()
                StaticInstance.gameManager.onGameStart()
            })
        } else if (i == 3) {
            this.node.parent.getChildByName("AddTimeLayer").getComponent(AddTimeLayer).auto = false
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AddTimeLayer)

        }
    }
    timeCount = 100
    setCurrentLevel() {

        this.currentLevel.getComponent(cc.Label).string = `第${DataManager.instance.level}关`
        if (DataManager.instance.stageModel) {
            this.currentLevel.active = false
            this.skills.children[0].active = true
            this.skills.children[1].active = true
            this.skills.children[3].active = true
            this.time.active = true
            this.timeCount = 600
            this.time.getComponent(cc.Label).string = this.formatTime(this.timeCount)
            switch (privacyChannel.channel) {
                case "oppo":
                case "oppo1":
                case "xm":
                    if (privacyChannel.getChannel().getCtrl("p1")) {
                        try {
                            privacyChannel.getChannel().showBanner()
                        } catch (e) {
                            console.error("p1控制", e)
                        }
                    }
                    if (privacyChannel.getChannel().getCtrl("p1")) {
                        this.schedule(() => {
                            try {
                                privacyChannel.getChannel().showBanner()
                            } catch (e) {
                                console.error("p1控制", e)

                            }

                        }, 5, cc.macro.REPEAT_FOREVER, 0)
                    }
                    if (privacyChannel.getChannel().getCtrl("p2")) {
                        this.schedule(() => {
                            try {
                                privacyChannel.getChannel().showInter(true)
                            } catch {
                                console.error("p2控制")
                            }

                        }, this.getRandomNumber(15, 60), cc.macro.REPEAT_FOREVER, 0)
                    }
                case "vivo":
                case "vivo1":
                    if (privacyChannel.getChannel().getCtrl("p1")) {
                        try {
                            privacyChannel.getChannel().showBanner()
                        } catch (e) {
                            console.error("p1控制", e)
                        }
                    }
                    if (privacyChannel.getChannel().getCtrl("p1")) {
                        this.schedule(() => {
                            try {
                                privacyChannel.getChannel().showBanner()
                            } catch (e) {
                                console.error("p1控制", e)
                            }

                        }, 5, cc.macro.REPEAT_FOREVER, 0)
                    }

                    if (privacyChannel.getChannel().getCtrl("p2")) {
                        this.schedule(() => {
                            try {
                                privacyChannel.getChannel().showInter(true)
                            } catch {
                                console.error("p2控制")
                            }

                        }, this.getRandomNumber(15, 60), cc.macro.REPEAT_FOREVER, 0)
                    }
                    if (privacyChannel.getChannel().getCtrl("p15")) {
                        this.schedule(() => {
                            if (DataManager.instance.level >= 3) {

                                try {
                                    privacyChannel.getChannel().showVideo(() => {

                                    })
                                } catch {
                                    console.error("p15控制")
                                }
                            }

                        }, 5, cc.macro.REPEAT_FOREVER, 0)

                    }



                    break
            }

        } else {
            this.time.active = false
            this.skills.children[3].active = false
            this.currentLevel.active = true
        }
    }
    refreshTime() {
        this.timeCount += 120
        this.time.getComponent(cc.Label).string = this.formatTime(this.timeCount)
        this.time.active = true
    }
    timeElapsed = 0
    protected update(dt: number): void {
        this.timeElapsed += dt;

        // 每秒执行一次
        if (this.timeElapsed >= 1 && DataManager.instance.status == ENUM_GAME_STATUS.RUNING && this.time.active && !this.node.parent.getChildByName("AddTimeLayer").active) {
            // 重置计时器
            this.timeElapsed = 0;
            this.timeCount--
            this.time.getComponent(cc.Label).string = this.formatTime(this.timeCount)

            if (this.timeCount <= 0) {
                this.time.active = false
                this.node.parent.getChildByName("AddTimeLayer").getComponent(AddTimeLayer).auto = true
                StaticInstance.uiManager.toggle(ENUM_UI_TYPE.AddTimeLayer)
                console.error("游戏失败")
            }
        }

    }
    formatTime(seconds: number): string {
        const minutes = Math.floor(seconds / 60); // 获取分钟数
        const remainingSeconds = seconds % 60; // 获取剩余秒数
        return `${minutes}：${remainingSeconds}`;
    }
}
